package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.dto.ElderPageQueryDto;
import com.zzyl.nursing.domain.pojo.Elder;
import com.zzyl.nursing.domain.vo.ElderVo;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.service.IElderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 老人Service业务层处理
 * @author alexis
 * @date 2025-05-19
 */
@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService {
    @Autowired
    private ElderMapper elderMapper;
    
    /**
     * 查询老人
     * @param id 老人主键
     * @return 老人
     */
    @Override
    public Elder selectElderById(Long id) {
        return elderMapper.selectById(id);
    }
    
    /**
     * 查询老人列表
     * @param elder 老人
     * @return 老人
     */
    @Override
    public List<Elder> selectElderList(Elder elder) {
        return elderMapper.selectElderList(elder);
    }
    
    /**
     * 新增老人
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int insertElder(Elder elder) {
        return elderMapper.insert(elder);
    }
    
    /**
     * 修改老人
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int updateElder(Elder elder) {
        return elderMapper.updateById(elder);
    }
    
    /**
     * 批量删除老人
     * @param ids 需要删除的老人主键
     * @return 结果
     */
    @Override
    public int deleteElderByIds(Long[] ids) {
        return elderMapper.deleteBatchIds(Arrays.asList(ids));
    }
    
    /**
     * 删除老人信息
     * @param id 老人主键
     * @return 结果
     */
    @Override
    public int deleteElderById(Long id) {
        return elderMapper.deleteById(id);
    }
    
    @Override
    public TableDataInfo<ElderVo> pageQuery(ElderPageQueryDto elderPageQueryDto) {
        // 1-动态条件分页查询elder：Page<elder>
        String name=elderPageQueryDto.getName();
        String idCardNo=elderPageQueryDto.getIdCardNo();
        Page<Elder> page=this.lambdaQuery()
                             .like(StrUtil.isNotBlank(name),Elder::getName,name)
                             .like(StrUtil.isNotBlank(idCardNo),Elder::getIdCardNo,idCardNo)
                             .eq(Elder::getStatus,1)
                             .orderByDesc(Elder::getCreateTime)
                             .page(Page.of(elderPageQueryDto.getPageNum(),elderPageQueryDto.getPageSize()));
        // 2-构建TableDataInfo对象填充List<ElderVo>
        List<Elder> elderList=page.getRecords();
        List<ElderVo> elderVoList=BeanUtil.copyToList(elderList,ElderVo.class);
        return TableDataInfo.<ElderVo>getDataTable(elderVoList,page.getTotal());
    }
}
